package cn.wolfcode.service.Impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private RoleMapper roleMapper;


    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Autowired
    private PermissionMapper permissionMapper;

    public void reload(){
        //从数据库中一次性将所有的权限信息查询出来
        List<Permission> permissions = permissionMapper.selectAll();
        Set<String> expressionset = new HashSet<>();
        for (Permission permission : permissions) {
            expressionset.add(permission.getExpression());
        }



        //将Controller中所有贴了注解的方法存到一个容器中
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        for (HandlerMethod method : values) {
            RequiredPermission annotation = method.getMethodAnnotation(RequiredPermission.class);
            if (annotation!=null){
                String expermission = annotation.expermission();
                if (!expressionset.contains(expermission)){
                    Permission permission = new Permission();
                    permission.setName(annotation.name());
                    permission.setExpression(annotation.expermission());
                    permissionMapper.insert(permission);
                }else {
                    expressionset.remove(expermission);
                }
            }
    }

        for (String expression : expressionset) {
            permissionMapper.deleteByExperssion(expression);
        }



    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Permission get(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        return  permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        List<String>list=permissionMapper.selectByEmpId(id);
        return list;
    }

}
